package com.hand.qt24953.order.app.service;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.hand.qt24953.order.constant.OrderStatus;
import com.hand.qt24953.order.domain.entity.Customer;
import com.hand.qt24953.order.domain.entity.Item;
import com.hand.qt24953.order.domain.entity.SoHeader;
import com.hand.qt24953.order.domain.entity.SoLine;
import com.hand.qt24953.order.domain.repository.CustomerRepository;
import com.hand.qt24953.order.domain.repository.ItemRepository;
import com.hand.qt24953.order.domain.repository.SoHeaderRepository;
import com.hand.qt24953.order.domain.repository.SoLineRepository;
import org.hzero.boot.imported.app.service.IDoImportService;
import org.hzero.boot.imported.infra.validator.annotation.ImportService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.util.CollectionUtils;

import java.io.IOException;
import java.util.Optional;

/**
 * @author qiutao 2019/8/5 19:58
 */
@ImportService(templateCode = "HODR-24953")
public class OrderImportService implements IDoImportService {
    private static Logger log = LoggerFactory.getLogger(OrderImportService.class);
    private final ObjectMapper objectMapper;
    private final SoHeaderRepository soHeaderRepository;
    private final SoLineRepository soLineRepository;
    private final CustomerRepository customerRepository;
    private final ItemRepository itemRepository;

    public OrderImportService(ObjectMapper objectMapper, SoHeaderRepository soHeaderRepository, SoLineRepository soLineRepository, CustomerRepository customerRepository, ItemRepository itemRepository) {
        this.objectMapper = objectMapper;
        this.soHeaderRepository = soHeaderRepository;
        this.soLineRepository = soLineRepository;
        this.customerRepository = customerRepository;
        this.itemRepository = itemRepository;
    }

    @Override
    public Boolean doImport(String data) {
        SoHeader soHeader;
        SoLine soLine;
        Customer customer;
        Item item;
        try {
            soHeader = objectMapper.readValue(data, SoHeader.class);
            customer = objectMapper.readValue(data, Customer.class);
            item = objectMapper.readValue(data, Item.class);
            if (!hasOrderHeader(soHeader.getOrderNumber())) {
                //查询用户id 公司id
                customer = customerRepository.select(Customer.FIELD_CUSTOMER_NUMBER, customer.getCustomerNumber()).get(0);
                soHeader.setCustomerId(customer.getCustomerId());
                soHeader.setCompanyId(customer.getCompanyId());
                //将中心转化成英文状态
                OrderStatus status = Optional.ofNullable(OrderStatus.parse(soHeader.getOrderStatus())).orElse(OrderStatus.NEW);
                soHeader.setOrderStatus(status.toString());
                soHeaderRepository.insertSelective(soHeader);
            }
            soHeader = soHeaderRepository.select(SoHeader.FIELD_ORDER_NUMBER, soHeader.getOrderNumber()).get(0);
            soLine = objectMapper.readValue(data, SoLine.class);

            item = itemRepository.select(Item.FIELD_ITEM_CODE, item.getItemCode()).get(0);
            soLine.setSoHeaderId(soHeader.getSoHeaderId());
            soLine.setItemId(item.getItemId());
            //备注字段统一写死：excel导入
            soLine.setDescription("excel导入");
            soLineRepository.insertSelective(soLine);
        } catch (IOException e) {
            log.error("导入错误", e);
        }
        return true;
    }

    private boolean hasOrderHeader(String orderNumber) {
        return !CollectionUtils.isEmpty(soHeaderRepository.select(SoHeader.FIELD_ORDER_NUMBER, orderNumber));
    }
}
